import 'dart:async';

import 'package:boss/textStyle/text_style.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class AddTimerText extends StatefulWidget {
  final String dateTime;
  final String title;
  final EdgeInsets margin;

  const AddTimerText({
    Key key,
    this.dateTime,
    this.title,
    this.margin,
  }) : super(key: key);

  @override
  _AddTimerTextState createState() => _AddTimerTextState();
}

class _AddTimerTextState extends State<AddTimerText> {
  Timer timer;
  DateTime startTime;

  String get text {
    startTime = DateTime.parse(widget.dateTime);

    var now = DateTime.now();

    var disTime = now.difference(startTime);

    var minutes = disTime.inMinutes;
    var seconds = disTime.inSeconds % 60;

    return '${StringUtil.singlePadLeftZero('$minutes')}:${StringUtil.singlePadLeftZero('$seconds')}';
  }

  @override
  void initState() {
    super.initState();

    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {});
    });
  }

  @override
  void dispose() {
    timer?.cancel();
    timer = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: widget.margin,
      height: 45.w,
      alignment: Alignment.centerLeft,
      child: Text(
        '${widget.title}： $text',
        style: TextStyleConstant().normal_1_28,
      ),
    );
  }
}
